Client = class('Client'):include(Stateful)

function Client:initialize(ip, port)
	print("initializing client")
	self.character_points = 0
	self.training_points = 0
	self:service_thread(ip, port)

	self.skill_cost_list = {
		["run"] = { ["train"] = 2, ["spec"] = 4 },
		["jump"] = { ["train"] = 2, ["spec"] = 6 },
		["light_melee"] = { ["train"] = 4, ["spec"] = 8 },
		["heavy_melee"] = { ["train"] = 8, ["spec"] = 12 },
		["light_ranged"] = { ["train"] = 4, ["spec"] = 6 },
		["heavy_ranged"] = { ["train"] = 8, ["spec"] = 14 },
		["akimbo"] = { ["train"] = 4, ["spec"] = 6 },
		["stealth"] = { ["train"] = 12, ["spec"] = 18 },
		["detect_stealth"] = { ["train"] = 12, ["spec"] = 14 },
		["locksmithing"] = { ["train"] = 6, ["spec"] = 12 },
		["tracking"] = { ["train"] = 4, ["spec"] = 8 },
		["trapping"] = { ["train"] = 6, ["spec"] = 12 },
		["melee_d"] = { ["train"] = 8, ["spec"] = 14 },
		["ranged_d"] = { ["train"] = 6, ["spec"] = 10 },
		["kidnapping"] = { ["train"] = 12, ["spec"] = 16 },
		["interrogation"] = { ["train"] = 10, ["spec"] = 14 },
		["escape"] = { ["train"] = 4, ["spec"] = 8 },
		["tolerance"] = { ["train"] = 8, ["spec"] = 12 },
		["elementalism"] = { ["train"] = 16, ["spec"] = 16 },
		["chaos"] = { ["train"] = 4, ["spec"] = 8 },
		["blessing"] = { ["train"] = 4, ["spec"] = 8 },
		["summoning"] = { ["train"] = 4, ["spec"] = 8 },
		["mentalism"] = { ["train"] = 4, ["spec"] = 8 },
		["demolitions"] = { ["train"] = 4, ["spec"] = 8 },
		["medicine"] = { ["train"] = 4, ["spec"] = 8 },
		["mechanics"] = { ["train"] = 4, ["spec"] = 8 },
		["cyber"] = { ["train"] = 4, ["spec"] = 8 },
		["psychology"] = { ["train"] = 4, ["spec"] = 8 },

	}

	self.train_list = {
	"run",
	"jump",
	"light_melee",
	"heavy_melee",
	"light_ranged",
	"heavy_ranged",
	"akimbo",
	"stealth",
	"detect_stealth",
	"locksmithing",
	"tracking",
	"trapping",
	"melee_d",
	"ranged_d",
	"kidnapping",
	"interrogation",
	"escape",
	"tolerance"
	}

	self.nat_train_list = {
	"elementalism",
	"chaos",
	"blessing",
	"summoning",
	"mentalism"
	}

	self.tech_train_list = {
	"demolitions",
	"medicine",
	"mechanics",
	"cyber",
	"psychology"
	}

	self:gotoState("guest")
end

function Client:draw()

end

function Client:update(dt)

	

	

end

function Client:service_thread(ip, port)
	print(ip)
	local s = [[
	require'enet'
	require'love.thread'
	local host = enet.host_create()
	local server = host:connect("]] .. tostring(ip) .. [["..":".. "]] .. tostring(port) .. [[") 
	local connect = true
	local init = false
	local pwd_sent = false
	local authed = false

	

	while connect do
		local event = host:service(100)
		local cmd = love.thread.getChannel("client_command")
		if cmd:peek() == "disconnect" then
			cmd:pop()
			server:disconnect()
			host:flush()
		end

		if cmd:peek() == "email_stream" then
			cmd:pop()
			local enc_email = cmd:pop()
			print(server:state())
			server:send("[email]"..enc_email)
		end

		if cmd:peek() == "pass_stream" then
			cmd:pop()
			local enc_pass = cmd:pop()
			server:send("[pass]"..enc_pass)
		end

		if cmd:peek() == "new_stat_stream" then
			cmd:pop()
			local stat = cmd:pop()
			server:send("[stat]"..stat)
		end

		if cmd:peek() == "stat_stream_finished" then
			cmd:pop()
			server:send("[stat_stream_finished]")
		end

		if cmd:peek() == "new_skill_train_stream" then
			cmd:pop()
			local skill = cmd:pop()
			server:send("[train]"..skill)
		end

		if cmd:peek() == "new_skill_spec_stream" then
			cmd:pop()
			local skill = cmd:pop()
			server:send("[spec]"..skill)
		end

		if cmd:peek() == "skill_stream_finished" then
			cmd:pop()
			server:send("[skill_stream_finished]")
		end

		if cmd:peek() == "say" then
			cmd:pop()
			local msg = cmd:pop()
			server:send("[say]"..msg)
		end

		if cmd:peek() == "team_selection" then
			cmd:pop()
			local team = cmd:pop()
			server:send("[team]"..team)
		end

		if cmd:peek() == "move_up" then
			cmd:pop()
			server:send("[move_up]")
		end

		if cmd:peek() == "move_down" then
			cmd:pop()
			server:send("[move_down]")
		end

		if cmd:peek() == "move_right" then
			cmd:pop()
			server:send("[move_right]")
		end

		if cmd:peek() == "move_left" then
			cmd:pop()
			server:send("[move_left]")
		end


		if event then
			if not init then			
				
					if event.type == "connect" then
						print("connected to ", event.peer)
						event.peer:send("hello world")
					elseif event.type == "receive" then
						print("Got message: ", event.data, event.peer)
						init = true					
					end
				
			end
			if init then
				
				if event.type == "receive" and authed == false then
					if event.data == "send_pwd" and not pwd_sent then						
						local lfc = love.thread.getChannel("lf_state")
						lfc:push("client_guest_pass")
						pwd_sent = true
					end

					if event.data == "login_reset" then
						local client_response = love.thread.getChannel("client_response")
						client_response:push("quit")
					end

					if event.data == "guest_authed" then
						local ga = love.thread.getChannel("guest_authed")
						ga:push("true")
						authed =true
						
					end
				end

				if event.type == "receive" and authed == true then
					print("pushing server command")
					local ch = love.thread.getChannel("server_commands")
					ch:push(event.data)
				end
		
			end
		end
	end
	]]

	local fd = love.filesystem.newFileData(s, "s.lua", "file")
	self.net_thread = love.thread.newThread(fd)
	self.net_thread:start()



end

function Client:keypressed(key, isRepeat)	


end

function Client:keyreleased(key)


end

function Client:mousepressed(x, y, button)

end

function Client:mousereleased(x, y, button)

end

function Client:textinput(text)

end

function Client:quit()
	
end

require'src.client_states.guest'